﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace UCL.Data
{
    /// <summary>
    /// 数据库帮助类工厂
    /// </summary>
    /// <remarks>2015-1-28 by sutroon</remarks>
    public class DatabaseHelperFactory
    {
        /// <summary>
        /// 获取指定提供程序名称的数据库帮助类
        /// </summary>
        /// <param name="providerName"></param>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        /// <remarks>2015-1-28 by sutroon</remarks>
        /// <remarks>2015-8-11 SoChishun 重构</remarks>
        public static IDatabaseHelper GetDatabaseHelper(string providerName, string connectionString = "")
        {
            Dictionary<string, string> providers = new Dictionary<string, string>() {
                { "System.Data.SqlClient","UCL.Data.SQLServerHelper" },
                { "MySql.Data.MySqlClient","UCL.Data.MySQLHelper" },
                { "System.Data.SQLite","UCL.Data.SQLiteHelper" },
                { "System.Data.OleDb","UCL.Data.OleDbHelper" },
            };
            if (!string.IsNullOrEmpty(providerName) && providers.Keys.Contains(providerName))
            {
                try
                {
                    return (IDatabaseHelper)Assembly.GetExecutingAssembly().CreateInstance(providers[providerName], false, BindingFlags.CreateInstance | BindingFlags.Public, null, new object[] { connectionString }, null, null);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return null;
        }
    }
}
